package com.whale.cloud.util.solr;

import lombok.extern.log4j.Log4j2;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;

import java.io.IOException;
import java.util.*;
/** Created by maoweidong on 2017/7/13. */
@Log4j2
public class SolrClient {

  private static CloudSolrClient cloudSolrClient = null;
  private static Collection<String> zkhost_collection;
  private static SolrProperties solrProperties;

  public void setSolrProperties(SolrProperties solrProperties) {
    this.solrProperties = solrProperties;
  }

  public static void init() {
    zkhost_collection = new ArrayList<>();
    String[] zkHosts = solrProperties.getZkHost().split(",");
    for (int i = 0; i < zkHosts.length; i++) {
      zkhost_collection.add(zkHosts[i]);
    }
    //连接集群
    if (cloudSolrClient == null) {
      try {
        cloudSolrClient = new CloudSolrClient(zkhost_collection, "/solr");
        cloudSolrClient.setDefaultCollection(solrProperties.getCollection());
        cloudSolrClient.setZkClientTimeout(solrProperties.getZkClientTimeout());
        cloudSolrClient.setZkConnectTimeout(solrProperties.getZkConnectTimeout());
        cloudSolrClient.connect();
      } catch (Exception e) {
        log.error("The URL of zkHost is not correct!");
        e.printStackTrace();
      }
    }
  }

  public String querySolr(
      String groups, HashMap<String, Object> sort, int pagenumber, int pagesize) {
    //    if (cloudSolrClient == null) {
    //      init();
    //    }
    StringBuilder sb = new StringBuilder();
    sb.append("NOT FR_ACTIVE:N").append(" AND ");
    HashMap<String, Object> Jsonmap = new HashMap<String, Object>();
    String json = null;
    try {
      sb.append(groups);
      log.info("===Solr===" + sb.toString());
      SolrQuery solrQuery = new SolrQuery(sb.toString());
      //排序
      if (sort != null && sort.size() > 0) {
        Set set = sort.keySet();
        if (set.contains("desc")) {
          List<String> sortList = (List<String>) sort.get("desc");
          for (int i = 0; i < sortList.size(); i++) {
            solrQuery.addSort(sortList.get(i), SolrQuery.ORDER.desc);
          }
        } else if (set.contains("asc")) {
          List<String> sortList = (List<String>) sort.get("asc");
          for (int i = 0; i < sortList.size(); i++) {
            solrQuery.addSort(sortList.get(i), SolrQuery.ORDER.asc);
          }
        }
      }
      //分页查询
      solrQuery.setStart((pagenumber - 1) * pagesize);
      solrQuery.setRows(pagesize);
      QueryResponse resp = cloudSolrClient.query(solrQuery, SolrRequest.METHOD.POST);
      SolrDocumentList hits = resp.getResults();
      Jsonmap.put("status", "查询成功");
      Jsonmap.put("documents", hits);
      Jsonmap.put("size", hits.getNumFound());
    } catch (Exception e) {
      Jsonmap.put("status", "查询失败");
      Jsonmap.put("e", e.getMessage());
      e.printStackTrace();
    }
    com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject(Jsonmap);
    json = jsonObject.toString();
    return json;
  }
}
